package com.ruoyi.project.system.doctor.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.doctor.domain.TableDoctor;
import com.ruoyi.project.system.doctor.service.ITableDoctorService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 医师信息Controller
 * 
 * @author ruoyi
 * @date 2023-09-13
 */
@Controller
@RequestMapping("/system/doctor")
public class TableDoctorController extends BaseController
{
    private String prefix = "system/doctor";

    @Autowired
    private ITableDoctorService tableDoctorService;
    @Autowired
    private IUserService userService;

    @RequiresRoles("manager")
    @GetMapping()
    public String doctor()
    {
        return prefix + "/doctor";
    }

    /**
     * 查询医师信息列表
     */
    @RequiresRoles("manager")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TableDoctor tableDoctor)
    {
        startPage();
        List<TableDoctor> list = tableDoctorService.selectTableDoctorList(tableDoctor);
        return getDataTable(list);
    }

    /**
     * 导出医师信息列表
     */
    @RequiresRoles("manager")
    @RequiresPermissions("system:doctor:export")
    @Log(title = "医师信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TableDoctor tableDoctor)
    {
        List<TableDoctor> list = tableDoctorService.selectTableDoctorList(tableDoctor);
        ExcelUtil<TableDoctor> util = new ExcelUtil<TableDoctor>(TableDoctor.class);
        return util.exportExcel(list, "医师信息数据");
    }

    /**
     * 新增医师信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存医师信息
     */
    @RequiresRoles("manager")
    @Log(title = "医师信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TableDoctor tableDoctor)
    {
        User user = new User();
        user.setRoleId(101L);
        user.setUserName(tableDoctor.getName());
        user.setLoginName(tableDoctor.getName());
        user.setUserType("00");
        user.setPassword("admin123");
        user.setPhonenumber(tableDoctor.getConnectPhone());
        user.setStatus("0");
        user.setDelFlag("0");
        boolean regFlag = userService.registerUser(user);
        if (!regFlag){
            return AjaxResult.error("该医师已存在");
        }
        if (tableDoctorService.selectTableDoctorList(tableDoctor).size()>0){
            return AjaxResult.error("该医师已存在");
        }
        tableDoctor.setSuoyigng(IdUtils.fastSimpleUUID());
        return toAjax(tableDoctorService.insertTableDoctor(tableDoctor));
    }

    /**
     * 修改医师信息
     */
    @RequiresRoles("manager")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TableDoctor tableDoctor = tableDoctorService.selectTableDoctorById(id);
        mmap.put("tableDoctor", tableDoctor);
        return prefix + "/edit";
    }

    /**
     * 修改保存医师信息
     */
    @RequiresRoles("manager")
    @Log(title = "医师信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TableDoctor tableDoctor)
    {
        return toAjax(tableDoctorService.updateTableDoctor(tableDoctor));
    }

    /**
     * 删除医师信息
     */
    @RequiresRoles("manager")
    @Log(title = "医师信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tableDoctorService.deleteTableDoctorByIds(ids));
    }
}
